/**
 ******************************************************************************
 * @file           : crc_utils.h
 * @brief          : CRC Utilities Header File
 ******************************************************************************
 * @attention
 *
 * CRC校验工具函数
 * 支持CRC8、CRC16、CRC32等常用算法
 *
 ******************************************************************************
 */

/* Define to prevent recursive inclusion -------------------------------------*/
#ifndef __CRC_UTILS_H
#define __CRC_UTILS_H

#ifdef __cplusplus
extern "C" {
#endif

/* Includes ------------------------------------------------------------------*/
#include <stdint.h>
#include <stdbool.h>

/* Exported types ------------------------------------------------------------*/
/* Exported constants --------------------------------------------------------*/

/* CRC8多项式 */
#define CRC8_POLY_DEFAULT   0x07    /* x^8 + x^2 + x + 1 */
#define CRC8_POLY_MAXIM     0x31    /* Dallas/Maxim */

/* CRC16多项式 */
#define CRC16_POLY_IBM      0x8005  /* x^16 + x^15 + x^2 + 1 */
#define CRC16_POLY_CCITT    0x1021  /* CCITT */
#define CRC16_POLY_MODBUS   0x8005  /* Modbus */

/* CRC32多项式 */
#define CRC32_POLY_DEFAULT  0x04C11DB7  /* IEEE 802.3 */

/* Exported macro ------------------------------------------------------------*/
/* Exported variables --------------------------------------------------------*/
/* Exported functions --------------------------------------------------------*/

/* CRC8校验 */
uint8_t crc8_calculate(const uint8_t *data, uint32_t len);
uint8_t crc8_calculate_poly(const uint8_t *data, uint32_t len, uint8_t poly);
bool crc8_verify(const uint8_t *data, uint32_t len, uint8_t expected_crc);

/* CRC16校验 */
uint16_t crc16_calculate(const uint8_t *data, uint32_t len);
uint16_t crc16_modbus(const uint8_t *data, uint32_t len);
uint16_t crc16_ccitt(const uint8_t *data, uint32_t len);
bool crc16_verify(const uint8_t *data, uint32_t len, uint16_t expected_crc);

/* CRC32校验 */
uint32_t crc32_calculate(const uint8_t *data, uint32_t len);
bool crc32_verify(const uint8_t *data, uint32_t len, uint32_t expected_crc);

/* 累积计算（分段数据） */
uint8_t crc8_update(uint8_t crc, const uint8_t *data, uint32_t len);
uint16_t crc16_update(uint16_t crc, const uint8_t *data, uint32_t len);
uint32_t crc32_update(uint32_t crc, const uint8_t *data, uint32_t len);

#ifdef __cplusplus
}
#endif

#endif /* __CRC_UTILS_H */
